import config from './config'

export function checkPlatform() {
	let ua : any = window.navigator.userAgent.toLowerCase();

	if (ua.match(/MicroMessenger/i) != 'micromessenger') {
		uni.showToast({
			title: '请在微信浏览器中打开'
		})
		return
	}
	var url = window.location.protocol + "//" + window.location.host;
	const pages = getCurrentPages();
	const lastPage = url + "/#/" + pages[pages.length - 1].route

	let redirect_uri = encodeURIComponent(lastPage);
	let oauthUrl : any = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + config.appid +
		`&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
	uni.setStorageSync("lastPage", lastPage);
	window.location = oauthUrl;

}

export function GetUrlParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}

export function goHome() {
	uni.navigateTo({
		url: '/pages/index/index'
	});
}

export function goUserCenter() {
	uni.navigateTo({
		url: '/pages/my/index'
	});
}

export function goFeedback() {
	uni.navigateTo({
		url: '/pages/other/feedback'
	});
}